package com.ocn.li.scmdy.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * TODO redis工具类
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/2
 */
@Component
public class RedisUtils {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private ValueOperations<String, String> valueOperations;
	@Autowired
	private HashOperations<String, String, Object> hashOperations;
	@Autowired
	private ListOperations<String, Object> listOperations;
	@Autowired
	private SetOperations<String, Object> setOperations;
	@Autowired
	private ZSetOperations<String, Object> zSetOperations;
	RedisProperties.Jedis jedis;
	/**
	 * 默认过期时长，单位：秒
	 */
	public final static long DEFAULT_EXPIRE = 60 * 60 * 72;
	/**
	 * 不设置过期时长
	 */
	public final static long NOT_EXPIRE = -1;

	public void set(String key, Object value, long expire) {
		valueOperations.set(key, toJson(value));
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
	}

	public void set(String key, Object value) {
		set(key, value, DEFAULT_EXPIRE);
	}

	public <T> T get(String key, Class<T> clazz, long expire) {
		String value = valueOperations.get(key);
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value == null ? null : fromJson(value, clazz);
	}

	public <T> T get(String key, Class<T> clazz) {
		return get(key, clazz, NOT_EXPIRE);
	}

	public String get(String key, long expire) {
		String value = valueOperations.get(key);
		if (expire != NOT_EXPIRE) {
			redisTemplate.expire(key, expire, TimeUnit.SECONDS);
		}
		return value;
	}

	public String get(String key) {
		return get(key, NOT_EXPIRE);
	}

	public void delete(String key) {
		redisTemplate.delete(key);
	}

	public Long delete(Collection<String> keys) {
		return redisTemplate.delete(keys);
	}

	/**
	 * 写入map类型
	 *
	 * @param key
	 * @param map
	 */
	public void setMap(String key, Map<Object, Object> map) {
		redisTemplate.opsForHash().putAll(key, map);
		redisTemplate.expire(key, NOT_EXPIRE, TimeUnit.SECONDS);
	}

  /**
   * 写入map类型
   *
   * @param key
   * @param map
   */
  public void setExpireMap(Object key, Map<String, Object> map,long expire) {
    redisTemplate.opsForHash().putAll(String.valueOf(key), map);
    redisTemplate.expire(String.valueOf(key), expire, TimeUnit.SECONDS);
  }

	/**
	 * 获取map类型
	 *
	 * @param key
	 * @return
	 */
	public Map<Object, Object> getMap(String key) {
		return redisTemplate.opsForHash().entries(key);
	}

	/**
	 * 给已有map添加值
	 *
	 * @param hashKey
	 * @param key
	 * @param value
	 */
	public void put(String hashKey, String key, Object value) {
		redisTemplate.opsForHash().put(hashKey, key, value);
	}

  /**
   * 给已有map添加值
   *
   * @param hashKey
   * @param key
   * @param value
   */
  public void putExpire(String hashKey, String key, Object value,long expire) {
    redisTemplate.opsForHash().put(hashKey, key, value);
    redisTemplate.expire(key, expire, TimeUnit.SECONDS);
  }

	/**
	 * 给已有map删除某项键值
	 * @param hashKey
	 * @param key
	 */
	public void removeFromMap(String hashKey,String key){
		redisTemplate.opsForHash().delete(hashKey,key);
	}

	/**
	 * 根据hash来获取map中指定key值
	 *
	 * @param hashKey
	 * @param key
	 * @return
	 */
	public Object getValueOfMap(String hashKey, String key) {
		return redisTemplate.opsForHash().get(hashKey, key);
	}

	/**
	 * Object转成JSON数据
	 */
	private String toJson(Object object) {
		if (object instanceof Integer || object instanceof Long || object instanceof Float ||
				object instanceof Double || object instanceof Boolean || object instanceof String) {
			return String.valueOf(object);
		}
		return JSONObject.toJSONString(object);
	}

	/**
	 * JSON数据，转成Object
	 */
	private <T> T fromJson(String json, Class<T> clazz) {
		return JSON.parseObject(json, clazz);
	}

    /**
     * @Author Li9527
     * @Description  解析所有map构造成Map<String,String>
     * @Date  10:38
     * @Param
     * @return
     **/
    public Map<String,String> mapConvert(Map<Object,Object> oldMap) {
        Map<String,String> map = new HashMap<>();
        Iterator<Map.Entry<Object, Object>> entries = oldMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<Object, Object> entry = entries.next();
            map.put(entry.getKey().toString(),this.toJson(entry.getValue()));
        }
        return map;
    }

}
